var L = require('./leafletwx')

export function createMap(contanter, options, callback) {
  global.wxNodes ??= {};
  global.wxNodes.nodeIdStack ??= [];
  global.wxNodes.nodeIdStack.push(contanter.__wxExparserNodeId__);
  global.wxNodes[global.wxNodes.nodeIdStack[global.wxNodes.nodeIdStack.length - 1]] = contanter;
  global.wx = contanter;
  global.wx._showingTileLayerIndex = 0;
  wx.createSelectorQuery().in(contanter).select('#leafletwx-overlay-canvas').fields({ 
    node: true, 
    size: true 
  }).exec((res) => { 
    var width = res[0].width; 
    var height = res[0].height; 
    contanter.overlayCanvas = res[0].node;
    options ??= {};
    options.preferCanvas = true
    let map = L.map(contanter, options);
    contanter.overlayCanvas.setPosition = 'setOverlayCanvasPosition';
    contanter.overlayCanvas.getPosition = 'getOverlayCanvasPosition';
    contanter.overlayCanvas.addEventListener = function(event, func, useCapture) {
        global.wxDOM.addEventListener(contanter.overlayCanvas, event, func, useCapture);
    },
    contanter.overlayCanvas.removeEventListener = function(event, func, useCapture) { 
        global.wxDOM.removeEventListener(contanter.overlayCanvas, event, func, useCapture);
    }
    if (callback != null) callback(map);
  })
};